export const fetchPromise = (url, data = {}, method = 'GET', headers = {}, type=`json`) => {
// export const fetchPromise = (params) => {
  console.log('fetchPromise', url)
  const options = {
    method,
    mode: 'no-cors',
    headers: {
       'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;',
       // 'Content-Type': 'application/json;charset=utf-8;',
      'Access-Control-Allow-Origin': 'https://www.pgyer.com'
    },
  };

  if (data !== {}) {
    options.body = JSON.stringify(data);
  }
return new Promise((resolve, reject) => {

  // fetch(url, options)
  //   .then(response => {
  //     if (!response.ok) {
  //       throw new Error(`HTTP error! Status: ${response.status}`);
  //     }
  //     return response.json();
  //   })
  //   .catch(error => {
  //     console.error('Fetch error:', error);
  //     throw error;
  //   });
  fetch(url,options).then(function (response) {
      let data = '';
      if (type === 'json')
        data = response.json();
      else
        data = response.text();
      return data;
    }).then(data => {
      if (type === 'json' && data.code==404 ) {
        console && console.error('请求异常:'+data.msg);
        reject('请求异常:'+data.msg);
      }else {
        resolve(data);
      }
    }).catch(function (ex) {
      console.log(ex)
      // console && console.error('请求异常:'+ex);
      reject('请求异常:'+ex);
    });

})

}

